<?php

namespace App\Http\Controllers\Api;

use App\Exceptions\ResponseException;
use App\Models\Goods;
use App\Models\GoodsSku;
use App\Models\GoodsSpec;
use App\Models\SeckillGoods;
use App\Models\SpecValue;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class GoodsController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

	/**
	 * 商品信息
	 * User: Clannad ~ ☆
	 * DataTime：2019/11/17 0017 16:24
	 * @param $id
	 * @throws ResponseException
	 */
    public function show($id)
    {
		//商品基本信息
    	$goods = Goods::query()
				->select('id','goods_name','thumb','price','description','kill_status')
				->where([
					'id'=>$id,
					'status'=>1
				])
				->first();

        if(!$goods){
        	throw new ResponseException(404,'商品不存在');
        }

        //获取秒杀信息
        $seckill_info = [];
        $goods['start_kill'] = 0;

        if($goods->kill_status == 1){
            $seckill_info = SeckillGoods::query()->where(['goods_id'=>$id,'kill_status'=>1])->first();

            $start_time = strtotime($seckill_info['start_time']);
            $end_time = strtotime($seckill_info['end_time']);

            //在这个世界范围内，就是开始秒杀的时间
            if(time() >= $start_time && time() < $end_time){
                $goods['start_kill'] = 1;
            }

            $seckill_info['start_time'] = date('H:i',$start_time);
            $seckill_info['end_time'] = date('H:i',$end_time);
        }

        //前台展示的规格信息
		$goodsSpec = GoodsSpec::query()
					->select('goods_id','spec_id','spec.spec_name',\DB::raw('GROUP_CONCAT(spec_value_id) as spec_value_id'))
					->leftJoin('spec','goods_spec.spec_id','=','spec.id')
					->where('goods_id',$id)
					->groupBy(['goods_spec.spec_id','goods_id','spec_id','spec.spec_name'])
					->get()->toArray();

        foreach ($goodsSpec as $k => $v) {
        	$spec_item = SpecValue::query()
						->select('id','value')
						->whereIn('id',explode(',',$v['spec_value_id']))
						->get()->toArray();

			$goodsSpec[$k]['spec_items'] = $spec_item;
        }

		//商品相册
		$goods_album = $goods->goods_album()->select('image')->get()->toArray();

        //商品的库存信息
        $goods_inventory = GoodsSku::query()
                    ->select(['sku_list','inventory','price'])
                    ->where([
                    'goods_id'=>$id,
                    ])->get()->toArray();

		$data = [
			'goods'=>$goods,
			'goods_spec'=>$goodsSpec,
			'goods_album'=>$goods_album,
            'goods_inventory'=>$goods_inventory,
            'seckill_info'=>$seckill_info
		];

		throw new ResponseException(200,'ok',$data);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
